﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game.event 
{
	import com.sng.stg.game.bullet.EnemyBullet;
	import com.sng.stg.game.bullet.PlayerBullet;
	import com.sng.stg.game.StageViewer;
	
	/**
	 * ...
	 * @author snega
	 */
	public class ClearStage extends GameEvent
	{
		
		public function ClearStage(time:int) 
		{
			super(time);
		}
		
		override public function execute(currStage:StageViewer):void {
			trace("[Event] (" + currStage._time + ") Hit ClearStage ");
			
			var max:int = 0, i:int = 0;
			
			// remove all 2d elements
			max = currStage._2dElement.numChildren;
			for (i = 0; i < max; i++) currStage._2dElement.removeChildAt(0);
			trace("clear 2d objects: " + max);
			
			// remove all bullets
			max = 0;
			var bullet:EnemyBullet = currStage._enemyBulletList._next;
			while (bullet != null) {
				bullet.remove();
				bullet = bullet._next;
				max++;
			}
			trace("remove enemy bullets: " + max);
			
			// remove all continuous event
			max = 0;
			var conEvent:ContinuousEvent = currStage._continuousEventList._next;
			while (conEvent != null) {
				conEvent.remove();
				conEvent = conEvent._next;
				max++;
			}
			trace("remove continuous event: " + max);
			
			// re-add player and its bullet
			currStage._2dElement.addChild(currStage._player);
			currStage._player.alpha = 1;
			currStage._player._notInvincible = true;
			
			var playerBullet:PlayerBullet = currStage._playerBullentList._next;
			while (playerBullet != null) {
				currStage._2dElement.addChild(playerBullet);
				playerBullet = playerBullet._next;
			}
		}
		
	}
	
}